package com.kanshu.ksgb.fastread.doudou.module.personal.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.base.baseui.CommonH5Activity;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.LoadingDialog;
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterService;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;

/**
 * Created by wcy on 2017\6\23 0023.
 *{@link HelpFeedbackActivity}
 */
@Deprecated
public class FeedbackActivity extends BaseActivity implements View.OnClickListener {

    EditText mEditInfo;
    EditText mEditContact;
    Button mSubmit;
    LoadingDialog mDialog;
    TextView mMyFeedBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback_layout);
        setTitle("意见与反馈");
        mEditInfo = (EditText) findViewById(R.id.edit_info);
        mEditContact = (EditText) findViewById(R.id.edit_contact);
        mDialog = new LoadingDialog(this, "正在提交中");
        mSubmit = (Button) findViewById(R.id.submit);
        mDialog.setCancelable(false);
        mSubmit.setOnClickListener(this);
        findViewById(R.id.tv_common_problems).setOnClickListener(this);
        TextView mMyFeedBack = new TextView(this);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        mMyFeedBack.setText("我的反馈");
        mMyFeedBack.setTextColor(Color.parseColor("#000000"));
        mMyFeedBack.setTextSize(15);
        addRightView(mMyFeedBack, lp);

        mMyFeedBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(FeedbackActivity.this, MyFeedbackActivity.class), 1000);
            }
        });
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_common_problems:
                Intent intent = new Intent();
                intent.setClass(this, CommonH5Activity.class);
                intent.putExtra(CommonH5Activity.EXTRA_PARAM_URL, "http://www.baidu.com");
                intent.putExtra(CommonH5Activity.EXTRA_PARAM_TITLE, "常见问题汇总");
                intent.putExtra(CommonH5Activity.EXTRA_PARAM_NEEDINJECTJS, true);
                startActivity(intent);
                break;
            case R.id.submit:
                submit();
                break;
        }
    }

    @Override
    protected void initStatusbar() {
        try {
            mImmersionBar = ImmersionBar.with(this);
            mImmersionBar.keyboardEnable(true);
            mImmersionBar.init();
            if (ImmersionBar.isSupportStatusBarDarkFont())
                mImmersionBar.statusBarDarkFont(true).init();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int statusBarHeight = DisplayUtils.getStatusBarHeight(this);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mTitle.getLayoutParams();
        params.topMargin = statusBarHeight;
        mTitle.setLayoutParams(params);
    }

    private void submit() {
        if (TextUtils.isEmpty(mEditInfo.getText().toString())) {
            ToastUtil.showMessage(this, "内容不能为空");
            return;
        }
        if (!NetUtils.isNetworkAvailable(this)) {
            ToastUtil.showMessage(this, getString(R.string.standard_net_tip));
            return;
        }
        mDialog.show();
        RetrofitHelper helper = RetrofitHelper.getInstance();
        Map<String, String> map = new HashMap<>();
        map.put("content", mEditInfo.getText().toString());
        map.put("contact", mEditContact.getText().toString().trim());
        map.put("phone_type", android.os.Build.MODEL);
        map.put("app_version", String.valueOf(Utils.getVersionCode(this)));
        helper.createService(PersonCenterService.class).submitFeedback(map)
                .compose(asyncRequest())
                .subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                ToastUtil.showMessage(FeedbackActivity.this, "反馈提交成功!");
                mDialog.dismiss();
                finish();
            }

            @Override
            public void onError(Throwable e) {
                mDialog.dismiss();
                ToastUtil.showMessage(FeedbackActivity.this, "提交失败！");
            }

            @Override
            public void onComplete() {
            }
        });
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Utils.hideSoftKeyboard(mEditInfo);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        Utils.hideSoftKeyboard(mEditInfo);
        Utils.hideSoftKeyboard(mEditContact);
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            finish();
        }
    }
}
